/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.home.presenter;

import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.liebang.entity.bo.SearchBo;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.feature.home.contract.FriendsContract;
import com.yiqi.liebang.feature.home.dependencies.friends.DaggerFriendsPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class FriendsPresenter implements FriendsContract.Presenter {

    private FriendsContract.View mView;

    @Inject

    FriendsContract.Model mModel;


    public FriendsPresenter(FriendsContract.View mView) {
        this.mView = mView;
        DaggerFriendsPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void searchFriend(String content, int pageIndex, int pageZize) {
        mModel.searchFriends(new SearchBo(content, pageIndex, pageZize))
//                .compose(mView.bindToLife())
                .subscribe(new Observer<SearchFriendBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
//                        mView.showLoading(d);
                        Logger.e("onSubscribe");
                    }

                    @Override
                    public void onNext(SearchFriendBo couponBoPageData) {
//                        mView.dismissLoading();
                        Logger.e("onNext");
                        if (couponBoPageData == null) return;
                        if (couponBoPageData.getUserFriend() != null && couponBoPageData.getUserFriend().size() > 0) {
                            mView.showFriendsListSuccess(couponBoPageData.getUserFriend());
                        } else {
                            mView.showFriendsListEmpty();
                        }

                        if (couponBoPageData.getFriend() != null && couponBoPageData.getFriend().size() > 0) {
                            mView.showPeopleListSuccess(couponBoPageData.getFriend());
                        } else {
                            mView.showListEmpty();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e("onError");
                        mView.showFriendsListError(e.getMessage());
//                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
//                        mView.dismissLoading();
                        Logger.e("onComplete");
                    }
                });
    }

    @Override
    public void searchFriendMore(String content, int pageIndex, int pageZize) {
        mModel.searchFriends(new SearchBo(content, pageIndex, pageZize))
                .compose(mView.bindToLife()).subscribe(new Observer<SearchFriendBo>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SearchFriendBo couponBoPageData) {

                mView.showFriendsListMoreSuccess(couponBoPageData.getUserFriend());
            }

            @Override
            public void onError(Throwable e) {
                mView.showFriendsListMoreError(e.getMessage());
            }

            @Override
            public void onComplete() {
            }
        });
    }

    @Override
    public void searchExpert(String content, int pageIndex, int pageZize) {
        mModel.searchExpert(new SearchBo(content, pageIndex, pageZize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<SearchFriendBo.UserFriendBean>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<SearchFriendBo.UserFriendBean> couponBoPageData) {

                if (couponBoPageData != null && couponBoPageData.size() > 0) {
                    mView.showFriendsListSuccess(couponBoPageData);
                } else {
                    mView.showFriendsListEmpty();
                }

                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {

                mView.showFriendsListError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void searchExpertMore(String content, int pageIndex, int pageZize) {
        mModel.searchExpert(new SearchBo(content, pageIndex, pageZize))
                .compose(mView.bindToLife()).subscribe(new Observer<List<SearchFriendBo.UserFriendBean>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<SearchFriendBo.UserFriendBean> couponBoPageData) {

                mView.showFriendsListMoreSuccess(couponBoPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.showFriendsListError(e.getMessage());
            }

            @Override
            public void onComplete() {
            }
        });
    }
}
